package com.liholin.springboot.sample.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class ViewController {

    @GetMapping("page")//
    public String getView() {
        return "view";
    }

    @PostMapping("data")
    public ModelAndView data() {
        ModelAndView modelAndView = new ModelAndView("view");
        modelAndView.addObject("str1", "我叫xxx");
        modelAndView.addObject("str2", "你好");
        return modelAndView;
    }

    @GetMapping("view/{id}")
    public ModelAndView getViewAndParam(@PathVariable("id") int id,
                                        @RequestParam(
                                                value = "name",//参数
                                                required = false, //是否必传
                                                defaultValue = "")// 默认值
                                        String name) {
        ModelAndView modelAndView = new ModelAndView("view");
        modelAndView.addObject("str1", "我叫xxx");
        modelAndView.addObject("str2", "你好");
        modelAndView.addObject("str3", id);
        modelAndView.addObject("str4", name);
        return modelAndView;
    }
}
